#!/bin/bash

#script to gather statistics about versions of matlab used

. /opt/cluster/pim_lib

numfiles=$(ls $pim_datadir/ment*|wc -l)

echo "Examining $numfiles files..." >&2

count=0
for f in $pim_datadir/ment*
do 
	. $f
	if echo $pid_cmdline |grep '\<MATLAB\>' >/dev/null
	then 
		version=$(echo "$pid_cmdline" |awk -F/ '{print $3}')
		printf "%s\t%s\t%s\n" $pid_owner $pid_vsize "$version"
		echo -n "M" >&2
	else
		echo -n "." >&2
	fi
	let 'count++'
	if [ $count -ge $numfiles ]
	then
		echo >&2
	fi
done | awk '
{
	if (index(vforuser[$1], $3) == 0) {
		vforuser[$1]=vforuser[$1] " " $3
	}
	vercount[$3]+=1
	if (index(uforvers[$3],$1) == 0) {
		uforvers[$3]=uforvers[$3] " " $1
	}
}
END {
	#print "Versions of MATLAB used by users:"
	#printf ("Versions of MATLAB used by users:\n")
	#for (u in vforuser) {
#		printf ("%-10s uses MATLAB versions: %s\n", u, vforuser[u])
#	}
#	printf ("\nVersions of MATLAB used by users:\n")
	for (v in vercount) {
		printf ("%-12s used %3d times by %s\n", v, vercount[v], uforvers[v])
	}
}' |sort -b -k3nr,3

